import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:laos_mall/common/base/page_size_controller.dart';
import 'package:laos_mall/common/style/app_style.dart';
import 'package:laos_mall/common/style/extension_util.dart';
import 'package:laos_mall/common/widght/appbar_widget.dart';
import 'package:laos_mall/common/widght/item_widget.dart';
import 'package:laos_mall/pages/main/config.dart';

import '../../app_route.dart';
import '../../common/config/app_const.dart';
import '../../common/widght/image_widget.dart';
import '../../common/widght/list_title_widget.dart';
import 'logic.dart';

class OrderPage extends StatelessWidget {
  OrderPage({Key? key}) : super(key: key);

  final logic = Get.put(OrderLogic());

  @override
  Widget build(BuildContext context) {
    var everyW = (ScreenUtil().screenWidth - 40.w) / 4.0;
    return Scaffold(
      appBar: AppBarWidget(text: "my_orders".tr),
      body: Column(
        children: [
          TabBar(
            isScrollable: true,
            labelPadding: EdgeInsets.zero,
            indicatorColor: Colors.transparent,
            tabs: logic.tabBarNames
                .asMap()
                .entries
                .map(
                  (e) =>
                  Tab(text: e.value.tr).addContainer(
                      color: Colors.transparent,
                      width: everyW,
                      alignment: Alignment(-1 + 2.0 / 3.0 * e.key, 1)),
            )
                .toList(),
            controller: logic.tabController,
          ),
          TabBarView(
            controller: logic.tabController,
            children: List.generate(4, (index) => OrderItemPage(index: index,tag: logic.tabBarNames[index],)),
          ).addPadding(EdgeInsets.symmetric(horizontal: 20.w)).addExpand()
        ],
      ),
    );
  }
}

class OrderItemPage extends StatefulWidget {
  final int index;

  const OrderItemPage({super.key, required this.index, required this.tag});

  final String tag;

  @override
  State<OrderItemPage> createState() => _OrderItemPageState();
}

class _OrderItemPageState extends State<OrderItemPage> {
  late OrderItemLogic logic;

  @override
  void initState() {
    final OrderItemLogic logic = Get.put(
        OrderItemLogic(widget.tag), tag: widget.tag);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<OrderItemLogic>(builder: (logic) {
      return PageSizeView(
      itemBuilder: (_, index, data) {

        bool isOnline = data.pay_type_id == 1 ? true : false;
        String timeStr = "";
        String payBtnName = "go_pay".tr;
        switch(data.order_status){
          case 0:
            timeStr = "${"order_time".tr}${data.add_time ?? ""}";
            payBtnName = "go_pay".tr;
          case 1:
            timeStr = "${"payment_time".tr}${data.pay_time ?? ''}}";
            payBtnName = "";
          case 2:
            timeStr = "${"ship_time".tr}${data.shipping_time}";
          case 3: //已取消
            timeStr = "${"delivery_time".tr}${data.received_time}";
        }
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 20.h,),
            ItemWidget(
              prefixItem: isOnline ? "online_order".tr : "in_store_order".tr,
              suffixItem: data.order_status_name,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              titleStyle: AppStyle.textSmallStyle,
              subtitleStyle: AppStyle.textSmallStyle,
            ),
            SizedBox(
              height: 8.h,
            ),
            ItemWidget(
              prefixItem: "order_id".tr,
              mainAxisAlignment: MainAxisAlignment.start,
              titleStyle: AppStyle.textSmallStyleLightGray,
              subtitleStyle: AppStyle.textSmallStyleLightGray,
              suffixItem: data.order_sn,
            ),
            Text(
              timeStr,
              style: AppStyle.textSmallStyleLightGray,
            ),
            ListView.builder(
              itemBuilder: (_, subIndex) {
                var item  = data.items?[subIndex];
                return ListTitleWidget(
                  maxLeadingW: 88.w,
                  leading: ImageWidget(
                    url: item?.pic_thumb,
                    fit: BoxFit.cover,
                  ).addContainer(
                    color: Colors.transparent,
                    width: 88.w,
                    height: 88.w,
                  ),
                  title: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(item?.product_name ?? "",maxLines: 1,overflow: TextOverflow.ellipsis,)
                          .addPadding(EdgeInsets.only(left: 16.w)),
                      Text(
                        item?.sku_value ?? "",
                        style: AppStyle.subtitleLightStyle,
                      ).addPadding(EdgeInsets.only(left: 16.w, top: 9.h,bottom: 14.h)),
                      Text(
                        "${Config.symbol}${item?.price}",
                        style: AppStyle.textStyle
                            .copyWith(fontWeight: FontWeight.bold),
                      ).addPadding(EdgeInsets.only(left: 16.w))
                    ],
                  ),
                  paddingOfLeft: EdgeInsets.zero,
                  showArrow: false,
                ).addPadding(EdgeInsets.only(top: 12.h)).addGestureTap(() {
                  Get.toNamed(AppRouter.productDetail);
                });
              },
              shrinkWrap: true,
              padding: EdgeInsets.zero,
              itemCount: data.items?.length,
              physics: NeverScrollableScrollPhysics(),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text(
                  isOnline ? "online_payment".tr : "offline_payment".tr,
                  style: AppStyle.textSmallStyleLight,
                ),
                Text(
                  '${'total'.tr}:${Config.symbol}${data.total_amount}',
                  style: AppStyle.textSmallStyle,
                ).addPadding(EdgeInsets.symmetric(horizontal: 20.w)),
                ///todo: 这里我修改为了优惠金额 设计图是应付金额 只考虑到未支付状态
                // Text(
                //   'amount_due'.tr + ':${Config.symbol}1231',
                //   style: AppStyle.textSmallStyle,
                // )
                Text(
                  '${'discount'.tr}:${Config.symbol}${data.discount_amount}',
                  style: AppStyle.textSmallStyle,
                )
              ],
            ).addPadding(EdgeInsets.only(top: 12.h, bottom: 20.h)),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                AppButton.sizeBtn(() {}, "order_details".tr,
                    size: Size(93.w, 30.h),
                    style: AppButtonStyle.hollowBlack,
                    textStyle: AppStyle.textSmallStyle),
                SizedBox(width: 8.w,),
                AppButton.sizeBtn(() {
                  logic.cancelOrder(data.order_id);
                }, "cancel_order".tr,
                    size: Size(93.w, 30.h),
                    style: AppButtonStyle.hollowBlack,
                    textStyle: AppStyle.textSmallStyle).addVisibility(data.order_status == -1  || data.order_status == 0),
                SizedBox(width: 8.w,),
                AppButton.sizeBtn(() {}, "go_pay".tr,
                    size: Size(126.w, 30.h),
                    style: AppButtonStyle.normal,
                    textStyle: AppStyle.textSmallStyle).addVisibility(data.order_status == -1  || data.order_status == 0),
              ],
            ),
            SizedBox(height: 20.h,),
            Divider(),
          ],
        );
      },
      shrinkWrap: true,
      controller: logic,
      );
    },tag: widget.tag,);
  }

  _buildSkeletonItem(){
    return PageSizeView(
      itemBuilder: (_, index, data) {
        bool isOnline = index == 0 ? true : false;
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ItemWidget(
              prefixItem: isOnline ? "online_order".tr : "in_store_order".tr,
              suffixItem: "state",
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              titleStyle: AppStyle.textSmallStyle,
              subtitleStyle: AppStyle.textSmallStyle,
            ),
            SizedBox(
              height: 8.h,
            ),
            ItemWidget(
              prefixItem: "order_id".tr,
              mainAxisAlignment: MainAxisAlignment.start,
              titleStyle: AppStyle.textSmallStyleLightGray,
              subtitleStyle: AppStyle.textSmallStyleLightGray,
              suffixItem: "123123123",
            ),
            Text(
              "2025-02-19 10:36:26",
              style: AppStyle.textSmallStyleLightGray,
            ),
            ListView.builder(
              itemBuilder: (_, subIndex) {
                return ListTitleWidget(
                  maxLeadingW: 88.w,
                  leading: ImageWidget(
                    url: AppConst.imgUrl,
                    fit: BoxFit.cover,
                  ).addContainer(
                    color: Colors.transparent,
                    width: 88.w,
                    height: 88.w,
                  ),
                  title: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(" Mid-waist Flared Mid-waist Flared Jeans")
                          .addPadding(EdgeInsets.only(left: 16.w)),
                      Text(
                        "XS ｜ White",
                        style: AppStyle.subtitleLightStyle,
                      ).addPadding(EdgeInsets.only(left: 16.w, top: 9.h)),
                      Text(
                        "\$ 9999",
                        style: AppStyle.textStyle
                            .copyWith(fontWeight: FontWeight.bold),
                      ).addPadding(EdgeInsets.only(left: 16.w))
                    ],
                  ),
                  paddingOfLeft: EdgeInsets.zero,
                  showArrow: false,
                ).addPadding(EdgeInsets.only(top: 12.h)).addGestureTap(() {
                  Get.toNamed(AppRouter.productDetail);
                });
              },
              shrinkWrap: true,
              padding: EdgeInsets.zero,
              itemCount: 1,
              physics: NeverScrollableScrollPhysics(),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text(
                  isOnline ? "online_payment".tr : "offline_payment".tr,
                  style: AppStyle.textSmallStyleLight,
                ),
                Text(
                  '${'total'.tr}:${Config.symbol}1231',
                  style: AppStyle.textSmallStyle,
                ).addPadding(EdgeInsets.symmetric(horizontal: 20.w)),
                Text(
                  '${'amount_due'.tr}:${Config.symbol}1231',
                  style: AppStyle.textSmallStyle,
                )
              ],
            ).addPadding(EdgeInsets.only(top: 12.h, bottom: 20.h)),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                AppButton.sizeBtn(() {}, "order_details".tr,
                    size: Size(93.w, 30.h),
                    style: AppButtonStyle.hollowBlack,
                    textStyle: AppStyle.textSmallStyle),
                SizedBox(width: 8.w,),
                AppButton.sizeBtn(() {}, "cancel_order".tr,
                    size: Size(93.w, 30.h),
                    style: AppButtonStyle.hollowBlack,
                    textStyle: AppStyle.textSmallStyle),
                SizedBox(width: 8.w,),
                AppButton.sizeBtn(() {}, "go_pay".tr,
                    size: Size(126.w, 30.h),
                    style: AppButtonStyle.normal,
                    textStyle: AppStyle.textSmallStyle),
              ],
            ),
            SizedBox(height: 20.h,),
            Divider(),
          ],
        );
      },
      shrinkWrap: true,
      controller: logic,
    );
  }
}
